package cn.dong.edu.service.impl;

import cn.dong.edu.domain.dto.ChapterVo;
import cn.dong.edu.service.ChapterService;
import cn.dong.edu.domain.Chapter;
import cn.dong.edu.domain.Video;
import cn.dong.edu.domain.dto.VideoVo;
import cn.dong.edu.feign.VodService;
import cn.dong.edu.mapper.ChapterMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-07
 */
@Service
@Slf4j
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoServiceImpl videoService;

    @Autowired
    private VodService vodService;

    /**
     * 查询全部章节，包括小结
     *
     * @return
     */
    @Override
    public List<ChapterVo> findTotalById(String courseId) {
        // 结果
        List<ChapterVo> result = new ArrayList<ChapterVo>(15);

        // 获取章节信息
        QueryWrapper<Chapter> wrapper = new QueryWrapper<Chapter>();
        wrapper.eq("course_id", courseId);
        // 根据sort和id排序
        wrapper.orderByAsc("sort", "id");
        List<Chapter> chapters = super.baseMapper.selectList(wrapper);

        // 循环遍历章节信息
        for (Chapter chapter : chapters) {
            // 根据章节id，查询小结
            QueryWrapper<Video> query = new QueryWrapper<Video>();
            query.eq("chapter_id", chapter.getId());
            query.orderByAsc("sort", "id");
            List<Video> videos = videoService.list(query);


            // 填充小结vo
            List<VideoVo> videoVos = new ArrayList<VideoVo>(20);
            for (Video video : videos) {
                VideoVo videoVo = new VideoVo();
                videoVo.setId(video.getId());
                videoVo.setTitle(video.getTitle());
                videoVo.setVideoSourceId(video.getVideoSourceId());
                // 是否试看？
                videoVo.setFree(video.getIsFree() > 0);
                videoVos.add(videoVo);
            }

            // 填充章节vo
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);
            chapterVo.setChildren(videoVos);
            result.add(chapterVo);
        }

        return result;
    }

    /**
     * 删除章节，并且删除全部的课时（小结）
     *
     * @param chapterId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delete(String chapterId) {
        QueryWrapper<Video> wrapper = new QueryWrapper<Video>();
        wrapper.eq("chapter_id", chapterId);
        // 删除小结中的视频
        List<Video> list = videoService.list(wrapper);
        if (list != null) {
            // 获取id列表
            ArrayList<String> arrayList = new ArrayList<String>(20);
            for (int i = 0; i < list.size(); i++) {
                arrayList.add(list.get(i).getVideoSourceId());
            }
            vodService.deleteByIds(arrayList);
        }

        // 删除小结
        videoService.remove(wrapper);

        // 删除章节
        return baseMapper.deleteById(chapterId) > 0;
    }
}
